/**
 * <AiRmob-Framework; A simple augmented reality framework for android >
    Copyright (C) <2012>  <AiRmob Team>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rendercomponents;

public class TriangleMethods {
	
	/**
	 * Calculates the normals for a given set of vertices and stores the result in an float[].
	 * Is currently not used in the AiRmob-Engine.
	 * 
	 * @param vertices
	 * @param numVertices
	 * @param normals
	 */
	static void calculateNormals(float[] vertices, int numVertices, float[] normals){

		for (int i = 0; i < numVertices / 3; i += 3){

						float p1[] = {0, 0, 0};
						p1[0] = vertices[i * 3];
						p1[1] = vertices[i * 3 + 1];
						p1[2] = vertices[i * 3 + 2];

						float p2[] = {0, 0, 0};
						p2[0] = vertices[(i+1) *3];
						p2[1] = vertices[(i+1) * 3 + 1];
						p2[2] = vertices[(i+1) * 3 + 2];

						float p3[] = {0, 0, 0};
						p3[0] = vertices[(i+2) * 3];
						p3[1] = vertices[(i+2) * 3 + 1];
						p3[2] = vertices[(i+2) * 3 + 2];



						float u[] = {p2[0] - p1[0],
									 p2[1] - p1[1],
									 p2[2] - p1[2]};

						float v[] = {p3[0] - p1[0],
									 p3[1] - p1[1],
									 p3[2] - p1[2]};

						float n[] = {u[1] * v[2] - u[2] * v[1],
									 u[2] * v[0] - u[0] * v[2],
									 u[0] * v[1] - u[1] * v[0]};

						normals[i*3] = n[0];
						normals[i*3+1] = n[1];
					    normals[i*3+2] = n[2];

					    normals[(i + 1) * 3] = n[0];
					    normals[(i + 1) * 3 + 1] = n[1];
					    normals[(i + 1) * 3 + 2] = n[2];

					    normals[(i + 2) * 3] = n[0];
					    normals[(i + 2) * 3 + 1] = n[1];
					    normals[(i + 2) * 3 + 2] = n[2];
			};
			
	}

}
